package db.dbTables;

import core.objects.BaseObject;
import core.objects.Book;
import db.MySQLFormatDate;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BookTable extends AbstractDBTable {

    public static final String COLUMN_ID = "book_id";
    public static final String COLUMN_PUBLICATION_YEAR = "publication_year";
    public static final String COLUMN_PAGE_COUNT = "page_count";
    public static final String COLUMN_ISBN = "isbn";
    public static final String COLUMN_WIKI_URL = "wiki_url";
    public static final String COLUMN_THUMBNAIL_URL = "thumbnail_url";
    public static final String COLUMN_DESCRIPTION = "description";
    public static final String COLUMN_GOOGLE_RATING = "google_rating";
    public static final String COLUMN_LANGUAGE = "language";

    final public DbColumn id = new DbColumn(COLUMN_ID, Types.INTEGER, 11, false, this);
    final public DbColumn name = new DbColumn(COLUMN_NAME, Types.VARCHAR, 120, false, this);
    final public DbColumn publicationYear = new DbColumn(COLUMN_PUBLICATION_YEAR, Types.INTEGER, 6, true, this);
    final public DbColumn pageCount = new DbColumn(COLUMN_PAGE_COUNT, Types.INTEGER, 11, true, this);
    final public DbColumn isbn = new DbColumn(COLUMN_ISBN, Types.VARCHAR, 13, true, this);
    final public DbColumn wikiUrl = new DbColumn(COLUMN_WIKI_URL, Types.VARCHAR, 400, true, this);
    final public DbColumn thumbnailUrl = new DbColumn(COLUMN_THUMBNAIL_URL, Types.VARCHAR, 400, true, this);
    final public DbColumn description = new DbColumn(COLUMN_DESCRIPTION, Types.VARCHAR, 1500, true, this);
    final public DbColumn googleRating = new DbColumn(COLUMN_GOOGLE_RATING, Types.DOUBLE, 10, true, this);
    final public DbColumn language = new DbColumn(COLUMN_LANGUAGE, Types.VARCHAR, 45, true, this);

    public BookTable() {
        super("book");
        setIDColumn(id);
    }

    @Override
    public List<DbColumn> getDbColumns() {
        List<DbColumn> list = new ArrayList<DbColumn>();
        list.add(id);
        list.add(name);
        list.add(publicationYear);
        list.add(pageCount);
        list.add(isbn);
        list.add(wikiUrl);
        list.add(thumbnailUrl);
        list.add(description);
        list.add(googleRating);
        list.add(language);
        return list;
    }

    @Override
    public BaseObject convertSqlTableToBaseObject(ResultSet resultSet) {
        int id = 0, pageCount = 0;
        double googleRating = 0;
        String name = "", isbn = "", wikiURL = "", description = "", language = "";
        URL thumbnailUrl = null;
        Date publicationYear = null;

        try {
            id = resultSet.getInt(COLUMN_ID);
            name = resultSet.getString(COLUMN_NAME);
            String publicationStr = resultSet.getString(COLUMN_PUBLICATION_YEAR);
            publicationYear = publicationStr.equals("0") ? null : MySQLFormatDate.toDate(publicationStr);
            pageCount = resultSet.getInt(COLUMN_PAGE_COUNT);
            isbn = resultSet.getString(COLUMN_ISBN);
            wikiURL = resultSet.getString(COLUMN_WIKI_URL);
            thumbnailUrl = resultSet.getURL(COLUMN_THUMBNAIL_URL);
            description = resultSet.getString(COLUMN_DESCRIPTION);
            googleRating = resultSet.getDouble(COLUMN_GOOGLE_RATING);
            language = resultSet.getString(COLUMN_LANGUAGE);
        } catch (SQLException ex) {
            Logger.getLogger(CityTable.class.getName()).log(Level.SEVERE, null, ex);
        }

        return new Book(id, name, isbn, publicationYear, pageCount, wikiURL, thumbnailUrl, description, googleRating, language);
    }
}
